<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.sycit.cn
// +----------------------------------------------------------------------
// | Author: Peter.Zhang  <hyzwd@outlook.com>
// +----------------------------------------------------------------------
// | Date:   2017/9/11
// +----------------------------------------------------------------------
// | Title:  Contacts.php
// +----------------------------------------------------------------------
namespace app\index\controller;

use app\index\model\Customers;
use app\index\model\CustomersContact;
use think\Request;
use think\Session;
use think\Url;
use think\Db;

class Contacts extends Common_base
{
    public function _initialize()
    {
        // 是否有权限
        IS_ROOT([1,2])  ? true : $this->error('没有权限');
        return parent::_initialize(); // TODO: Change the autogenerated stub
    }

    //增加联系
    public function add() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $Customers = new Customers();
        $data = $Customers->field('cus_id')->where('cus_id',$id)->find();
        $this->assign('data', $data);
        return $this->fetch();
        //p($id);
    }

    //提交增加联系
    public function add_do() {
        $Request = Request::instance();
        $cusid = $Request->param('con_cus_id');
        if (!is_numeric($cusid) || empty($cusid)) {
            $this->error('参数错误！');
        }
        $Customers = new Customers();
        $ByCus = $Customers->where('cus_id',$cusid)->find();
        if (empty($ByCus)) {
            $this->error('这样瞎搞有意思吗？');
        }
        $model = new CustomersContact($_POST);
        $result = $model->allowField(true)->save();
        if ($result) {
            $this->success('添加成功', Url::build('customers/view',['id'=>$cusid]));
        } else {
            $this->error('添加失败，估计服务器有错误，请联系管理员！');
        }
    }

    //查看联系
    public function edit() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $model = new CustomersContact();
        $data = $model->where('con_id', $id)->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    //提交修改联系人
    public function edit_do() {
        $Request = Request::instance();
        $id = $Request->param('con_id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        if ($Request->isPost()) {
            $model = new CustomersContact();
            $result = $model->allowField(true)->save($_POST,['con_id'=>$id]);
            if ($result) {
                $this->success('修改联系人成功');
            } else {
                $this->error('修改操作错误！');
            }
        }
    }

    //删除联系
    public function deluser() {
        $Request = Request::instance();
        $id = $Request->param('id');
        if (!is_numeric($id) || empty($id)) {
            $this->error('参数错误！');
        }
        $model = new CustomersContact();
        $result = $model->save(['status'=>'-1'],['con_id' => $id]);
        if ($result) {
            $this->success('删除联系人成功');
        } else {
            $this->error('删除操作错误');
        }
    }

    //表单查询名称
    public function check_name() {
        $Request = Request::instance();
        if ($Request->isPost()) {
            $ByName = true;
            $name = $Request->param('name');
            $mobile = $Request->param('mobile');
            if (!empty($name)) {
                $ByName = Db::name('customers_contact')->where('con_name', $name)->find();
            }
            if (!empty($mobile)) {
                $ByName = Db::name('customers_contact')->where('con_mobile', $mobile)->find();
            }
            if ($ByName) {
                return false;
            } else {
                return true;
            }
        }
    }
}